package redpacket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: feiwang_6
 * @create: 2020/7/13 18:42
 * @description:
 */
public class RedPacket {
    //红包最小数
    private int MIN_MONEY = 1;
    //红包最大数
    private int MAX_MONEY = 100 * 100;
    //红包系数
    private double LIMIT_NUMBER = 2;

    private int tempCount = 0;
    
    /**
     * 生成红包
     * @param totalMoney
     * @param sum
     * @return
     */
    public List<Integer> skipRedPacket(int totalMoney, int sum){
        List<Integer> result = new ArrayList<>(sum);
        //最大额度限制
        int limitMax = (int) ((totalMoney / sum) * LIMIT_NUMBER);
        limitMax = limitMax > MAX_MONEY ? MAX_MONEY : limitMax;
        
        //红包拆分
        for(int i = 0; i < sum; i++){
            if(totalMoney < sum){
                System.out.println("1111111111");
            }
            System.out.println(totalMoney + "   "  + sum);
            int redPacket = createRedPacket(totalMoney, limitMax, sum - i);
            System.out.println("redPacket = " + redPacket);
            result.add(redPacket);
            totalMoney -= redPacket;
        }
        return result;
    }

    /**
     * 生成红包金额
     * @param totalMoney
     * @param sum
     * @return
     */
    public int createRedPacket(int totalMoney, int limitMax, int sum){
        if(sum == 1){
            return totalMoney;
        }
        
        int redMax = totalMoney > limitMax ? limitMax : totalMoney;
        
        int redNumber = (int)(Math.random() * redMax + MIN_MONEY);
        boolean checked = checkRedPacket(totalMoney - redNumber, sum - 1, redNumber, limitMax);
        if(!checked){
            System.out.println("递归次数 " + ++tempCount);
            return createRedPacket(totalMoney, limitMax, sum);
        }
        return redNumber;
    }
    
    public boolean checkRedPacket(int totalMoney, int sum, int redNumber, int limitMax){
        int avg = totalMoney / sum;
        if(avg < MIN_MONEY || avg > MAX_MONEY){
            return false;
        }
        
        if(redNumber > limitMax){
            return false;
        }
        
        return true;
    }
    
    
    public static void main(String[] args) {
        RedPacket redPacket = new RedPacket();
        List<Integer> list = redPacket.skipRedPacket(200 * 100, 200);
        
        int sum = 0;
        for(int enu : list){
            sum += enu;
        }
        System.out.println(list);
        System.out.println("总数： " + sum);
    }
}
